// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { AchiveItemId } from '../define/GameDefine';
import ItemGrid from '../items/ItemGrid';
import { Item } from '../object/Item';
import { ItemManager } from '../object/ItemManager';

const { ccclass, property } = cc._decorator;

const archiveText = ['身体健康吃嘛嘛香', '家庭幸福儿女双全', '财运亨通飞黄腾达', '勤勤恳恳打工人', '好好学习天天向上'];

@ccclass
export default class Result extends cc.Component {
  @property(ItemGrid)
  itemGrids: ItemGrid[] = [];

  @property(cc.Label)
  labels: cc.Label[] = [];

  @property(cc.Label)
  winLabel: cc.Label = null;

  @property(cc.Node)
  loseNode: cc.Node = null;

  // private winIds = [AchiveItemId.Apple, AchiveItemId.Medic, AchiveItemId.YaLing];
  onLoad() {
    //@ts-ignore
    const resultData = window.ResultData;
    const { winIds, isWin } = resultData;
    requestAnimationFrame(() => {
      if (isWin) {
        const items = [];
        for (let i = 0; i < winIds.length; i++) {
          const id = winIds[i];
          const item = new Item(id);
          items.push(item);
          this.itemGrids[i].setItem(item);
          this.labels[i].string = item.winText;
        }
        const { winType } = ItemManager.CheckSuccessed(items);
        const text = archiveText[winType - 1];
        this.winLabel.string = text;
      } else {
        this.node.active = false;
        this.loseNode.active = true;
      }
    });
  }

  start() {}

  // update (dt) {}
}
